// 本文件制作axios 请求API

import axios from "axios";


function loginUser(params) {
    return axios({
        method: 'post',
        url: 'login/',
        data: params,
    }).then((response) => {
        if (response.data.code == 200) {
            var token = response.data.token;
            var username = response.data.username;
            var nickname = response.data.nickname;
            window.localStorage.setItem("token", token);
            window.localStorage.setItem("username", username);
            window.localStorage.setItem('tokenStartTime', new Date().getTime())
            if (nickname) {
                window.localStorage.setItem("nickname", nickname);
                window.localStorage.setItem("surname", nickname.slice(0, 1));
            } else {
                window.localStorage.setItem("nickname", username);
                window.localStorage.setItem("surname", username.slice(0, 1));
            }
            return response
        } else {
            return response
        }
    }).catch((error) => {
        console.log(error);
    });
}

function logoutUser() {
    //清除用户登录信息
    localStorage.removeItem('token');
    localStorage.removeItem('username');
    localStorage.removeItem('nickname');
    return axios({
        method: 'delete',
        url: 'login/',
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}


export {
    loginUser,
    logoutUser,
}